package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.Student;
import com.ly.pojo.dto.StudentDTO;
import com.ly.service.StudentService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 黑渊白花
 * @ClassName StudentController
 * @date : 2025/2/10 21:47
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/page")
    public Object page(StudentDTO studentDTO){
        IPage<Student> page = studentService.getPage(studentDTO);
        return page;
    }

    @GetMapping("/{id}")
    @ExcludeToken
    public Result getById(@PathVariable Integer id){
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @GetMapping("/getByUserId/{id}")
    @ExcludeToken
    public Result getByUserId(@PathVariable Integer id){
        log.info("{}",id);
        Student student = studentService.getByUserId(id);
        return Result.success(student);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Student student){
        studentService.updateById(student);
        return Result.success();
    }

    //删除学生要判断是否是社长或是否是成员，是的话就不能删除否的话可以删除
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Student student){
        Boolean delete = studentService.isDelete(student);
        if(delete==false){
            return Result.warn("该学生是社长或是社团成员，删除失败");
        }
        return Result.success("删除成功");
    }
}
